<?php

	session_start();

	require_once("includes/connect.php");
	require_once("includes/function.php");

	$panngan2 = $_REQUEST["panngan2"];
	$panngan3 = $_REQUEST["panngan3"];
	$month_year = $_REQUEST["month_year"];
	$budget_year = $_REQUEST["budget_year"];
	$ex1_id = $_REQUEST["ex1_id"];
	$ex2_id = $_REQUEST["ex2_id"];
	$ex3_id = $_REQUEST["ex3_id"];

?>


<script type="text/javascript">
	$(document).ready(function(){
		$('#example').dataTable({
			"iDisplayStart": "<?php echo 0; ?>",
			"iDisplayLength": 100
		});
		
		$("#form_prnt").hide();

		$("#submit").click(function(){
			var month_year = "<?php echo $month_year; ?>";
			var panngan2 = "<?php echo $panngan2; ?>";
			var panngan3 = "<?php echo $panngan3; ?>";
			var ex1_id = "<?php echo $ex1_id; ?>";
			var ex2_id = "<?php echo $ex2_id; ?>";
			var ex3_id = "<?php echo $ex3_id; ?>";
			var budget_year = "<?php echo $budget_year; ?>";
			var task = "update_id";

			//alert (budget_year);

			
			$.post('process_salary.php', 
				{  month_year: month_year, panngan2: panngan2, panngan3: panngan3, ex1_id: ex1_id, ex2_id: ex2_id, ex3_id: ex3_id, budget_year: budget_year, task: task },
				function(data) { $("#form_prnt").show();$("#ppp").html(data);alert("บันทึกข้อมูลเรียบร้อย"); }
			);
			
			
		});

		$("#printer").click(function(e){
			//alert("dafsadfs");

			e.preventDefault();
			window.location.href="prt_salary.php?id=4567658";

		});
	});

	function edit_salary(id, salary) {
				
		var month_year = "<?php echo $month_year; ?>";
		var panngan2 = "<?php echo $panngan2; ?>";
		var panngan3 = "<?php echo $panngan3; ?>";
		var ex1_id = "<?php echo $ex1_id; ?>";
		var ex2_id = "<?php echo $ex2_id; ?>";
		var ex3_id = "<?php echo $ex3_id; ?>";
		var budget_year = "<?php echo $budget_year; ?>";

		$('<div>').dialog2({
			autoOpen: true,
			title: "แก้ไขข้อมูลเงินเดือน",
			content: "dlg_edit_salary.php?id=" + id + "&month_year=" + month_year + "&panngan2=" + panngan2 + "&panngan3=" + panngan3 + "&ex1_id=" + ex1_id + "&ex2_id=" + ex2_id + "&ex3_id=" + ex3_id + "&salary=" + salary + "&budget_year=" + budget_year,
			id: "modal-edit-salary",
			//	height:140
			//resizable: false,
			//width:1000
		});

	}
</script>

					<?php 
						$db = NEW myDBS();
						$db->selectDB("account");
					?>
<div class="panel panel-success">
	<div class="panel-heading"><h4>&nbsp;</h4></div>
		<div class="panel-body">
			<div class="row-fluid">	
				<div class="control-group span2">
				<label class="control-label" for="prefix">ปีงบประมาณ</label>
				<?php echo $budget_year; ?>
				</div>
				<div class="control-group span4">
				<label class="control-label" for="panngan2">แผนงาน</label>
				<?php 
					//echo $rows->{$field_name};
					echo $db->get_name("panngan2_id", "panngan2_name", "panngan2", $panngan2); 
				?>

				</div>
				<div class="control-group span4">
				<label class="control-label" for="panngan2">งาน</label>
				<?php 
					echo $db->get_name("panngan3_id", "panngan3_name", "panngan3", $panngan3); 
					//echo ; 
					?>

				</div>
			</div>
			<div class="row-fluid">	
				<div class="control-group span4">
				<label class="control-label" for="prefix">หมวด / ลักษณะ</label>
				<?php 
					echo $db->get_name("expenses1_id", "expenses1_name", "expenses_lvl1", $ex1_id); 
				?>
				</div>
				<div class="control-group span4">
				<label class="control-label" for="panngan2">ประเภท</label>
				<strong>
				<?php 
					echo $db->get_name("expenses2_id", "expenses2_name", "expenses_lvl2", $ex2_id); 
				?>
				</strong>

				</div>
				<div class="control-group span4">
				<label class="control-label" for="panngan2">รายการ</label>
				<strong>
				<?php 
					echo $db->get_name("expenses3_id", "expenses3_name", "expenses_lvl3", $ex3_id); 
				?>
				</strong>

				</div>
			</div>
		</div>
	</div>
</div>

<?php
	require_once("includes/connect.php");
	require_once("includes/function.php");

	//echo $panngan2, $panngan3;

	$db = NEW myDBS;
	$db->selectDB("hr_udoncity");

	$sql = "SELECT position.id, MAX( position_history_date ) 
			FROM position, position_history
			WHERE position_history.id = position.id
			AND panngan2_id = '$panngan2' 
			AND panngan3_id = '$panngan3'
			GROUP BY position.id
			";

	//echo $sql;
	$results = $db->runSQL($sql);
?>
	<table id="example" class="table table-bordered" width="100%" >
		<caption><br></caption>
		<thead>
			<tr>
				<th>#</th>
				<th>เลขประจำตัว</th>
				<th>ชื่อ - นามสกุล</th>
				<th>เงินเดือน</th>
				<th>action</th>
				<th>ข้อมูล</th>
			</tr>
		</thead>
		<tbody>
<?php
	$i=0;
	while ($rows = $db->fetch_object($results)) {
		$sql = "SELECT person.id, fname, lname, salary FROM person, position_history
				WHERE position_history.id = person.id
				AND person.id = '". $rows->id ."'
				";
		$results1 = $db->runSQL($sql);

		$rows1 = $db->fetch_object($results1);

		//echo $rows1->id, $rows1->fname, $rows1->lname, $rows1->salary."<br>";
		$i++;
		echo "<tr>
				<td>$i</td>
			  <td>$rows1->id</td>
			  <td>$rows1->fname&nbsp;&nbsp;&nbsp;$rows1->lname</td>
			  <td align='right'>".number_format($rows1->salary, 2)."</td>
			  <td><button type='button' class='btn' onclick='javascript:edit_salary($rows1->id, $rows1->salary)'>Click</button></td>
			  <td>ดู</td>
			  </tr>
			  ";
	}


?>
</tbody>

</table>
<p class="center">
		<button  id="submit" class="btn btn-primary"><i class="icon-ok icon-white"></i> บันทึกฎีกาเงินเดือน</button>

	<a href="index.php?pages=home" role="button" class="btn"><i class="icon-remove"></i>ยกเลิก</a>

	<form action="prt_salary.php" method="post"  target="_blank" id="form_prnt">
	<div id="ppp"></div>
	<input type="hidden" name="month_year" value="<?php echo $month_year; ?>">
	<input type="hidden" name="panngan2" value="<?php echo $panngan2; ?>">
	<input type="hidden" name="panngan3" value="<?php echo $panngan3; ?>">
	<input type="hidden" name="ex1_id" value="<?php echo $ex1_id; ?>">
	<input type="hidden" name="ex2_id" value="<?php echo $ex2_id; ?>">
	<input type="hidden" name="ex3_id" value="<?php echo $ex3_id; ?>">
	<input type="hidden" name="budget_year" value="<?php echo $budget_year; ?>">
	<button  type="submit" class="btn btn-info"><i class="icon-print icon-white"></i> พิมพ์ฎีกาเงินเดือน</button>
	</form>
</p>